package com.example.zhidao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhidao.dao.BlogMapper;
import com.example.zhidao.dao.ViewsMapper;
import com.example.zhidao.model.po.Views;
import com.example.zhidao.service.ViewsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

@Service
public class ViewsServiceImpl extends ServiceImpl<ViewsMapper, Views> implements ViewsService {

    @Resource
    BlogMapper blogMapper;

    @Resource
    ViewsMapper viewsMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addview(Views views) {
        try{
            viewsMapper.insert(views);
            blogMapper.addView(views.getBid());
        }catch (Exception e){
            // 回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
}
